<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box1 {
            width: 400px;
            height: 100px;
            border: 1px solid black;
            margin: 100px auto;
            position: relative;
        }
         /* 方法一 */
         .box2 {
             width: 100px;
             height: 100px;
             background-color: pink;
             /* 利用绝对定位的性质 */
             position: absolute;
             left: 50%;
             margin-left: -50px;
         }
         /* 方法二 */
         .box3 {
             width: 100px;
             height: 100px;
             background-color: pink;
             /* 设置margin属性 */
             /* 此方法里，父盒子宽度要大于子盒子宽度，才能给子盒子设置水平居中 */
             margin: auto;
         }

    </style>
</head>
<body>
    <!-- 盒子水平居中 -->
    <!-- 盒子1 -->
    <div class="box1">
        <div class="box2"></div>
    </div>
    <!-- 盒子2 -->
    <div class="box1">
        <div class="box3"></div>
    </div>
</body>
</html>